<template>
	<view class="container">
		<view class="avatar">
			<!-- 上传组件 -->
			<u-upload ref="img_logo" name="img_logo" :maxCount="1" :fileList="filelist" width="100" height="100"
				@delete="del" @afterRead="read" />
		</view>

		<view class="secetion">
			<u-form labelPosition="left" labelWidth="60" :model="brank" :rules="rules" ref="product">
			
				
				<!-- 名称 -->
				<u-form-item label="名称" prop="name">
					<u-input v-model="brank.name"  placeholder="请输入商品分类名称" />
				</u-form-item>
				<u-form-item label="门店" prop="store" borderBottom @click="showstore = true">
					<u-input v-model="brank.store.name" disabled disabledColor="#ffffff" placeholder="请选择所属门店"
						border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
			
				<!-- 品牌描述 -->
				<u-form-item label="描述" prop="content" borderBottom>
					<u-textarea placeholder="请输入商品分类描述" v-model="brank.content" count></u-textarea>
				</u-form-item>

				
				<!-- 提交按钮 -->
				<u-button text="修改商品分类" icon="pushpin" size="small" type="success"  @click="product" />
			</u-form>
			<u-popup :show="showstore" @close="closestore" @open="openstore">
				<view>
					<u-list height="560px">
						<u-list-item v-for="(item, index) in stolist" :key="index">
							<view class="branktypename" @click="storeSelect(item)">
								<u-cell :title="item.name">

								</u-cell>
							</view>
						</u-list-item>
					</u-list>
				</view>
			</u-popup>

		</view>
		<!-- 消息提醒 -->
		<u-toast ref="notice"></u-toast>
	</view>
</template>
  
<script>
export default {
	onLoad(options) {

		this.ptypeid = options.ptypeid ? options.ptypeid : 0,
		this.xzstoid = options.xzstoid ? options.xzstoid : 0,
		console.log(options)
		
		this.auth = this.$u.auth.check();

		this.goodsinfo()
		this.storelist()


	
	},

	onReady() {

		this.$refs.product.setRules(this.rules)


	},
	created() {

		this.timer = setInterval(() => {

			let a = this.$u.count.check()

		}, 15000);

	},
	beforeDestroy() {
		clearInterval(this.timer)
	},
	data() {
		return {
			showstore:false,
			stolist:[],
			xzstoid:"",
			newthumbArr:[],
			filelist: [],
			auth: {},
			stoid: null,
			img_logo: "",
			piclist:[],
			name: "",
			ptypeid:0,
			brank: {
				id:0,
				content:"",
				img_logo:"",
				name:"",
				store:{}
			},
			showbrank: false,
			rules: {
				content: {
					required: true,
					message: "不低于3个字",
					trigger: ["change"],
				},
				
				name: {
					required: true,
					message: "请填写商品分类名称",
					trigger: ["blur", "change"],
				},
				img_logo: {
					required: true,
					message: "请填上传商品分类图片",
					trigger: ["blur", "change"],
				},
			
			},

		};
	},

	methods: {
		storeSelect(e) {
			this.brank.store.name = e.name;
			this.stoid = e.id;
			this.showstore = false
		},
		openstore() {
			this.showstore = true;
			// console.log('close');
		},

		closestore() {
			this.showstore = false;
			// console.log('close');
		},
		async storelist() {
			let data = {
				adminid: this.auth.id,
				stoid: this.auth.stoid,
			};
			let result = await this.$u.api.product.storelist(data);

			if (result.code) {
				this.stolist = result.data;
			}
		},
		async del(e) {

		
					let data = {
						thumb: e.file.url,
						ptypeid:this.ptypeid
					}
					var result = await this.$u.api.product.delprotypethumb(data)

					if (result.code) {
						this.$refs.notice.show({
							type: "success",
							message: result.msg,
							duration: 1000,
							complete: () => {
						
									this.filelist = []
									this.brank.thumb_text = ""

								
								
						
							}
						});
						return false;
					}




		

		},
		async read(event) {

			//组装上传数据
			var data = {
				filePath: event.file.thumb,
				name: "product_img",
				header: {},
				
			};

			//调用接口
			var result = await this.$u.api.product.thumbs(data);

			if (result.code) {

				// 合并对象
				this.filelist = [{ url: result.data }]


				this.brank.thumb_text = result.data
				this.newthumbArr = this.newthumbArr.concat(result.data)







			} else {
				this.$refs.notice.show({
					type: "error",
					message: result.msg,
				});
				return false;
			}
		},
	
		
		product() {
			this.$refs.product.validate().then(async () => {

				var data = {
					id:this.ptypeid,
					adminid: this.auth.id,
					name: this.brank.name,
					thumb: this.brank.thumb_text,
					content: this.brank.content,
					stoid:this.stoid ? this.stoid : this.xzstoid,
					mdid:this.xzstoid,
					newthumbArr:this.newthumbArr
				};

				
				let result = await this.$u.api.product.goodstypeedit(data);

				if (result.code) {
					this.$refs.notice.show({
						type: "success",
						message: result.msg,
						duration: 1000,
						complete: () => {
							//返回上一个界面
							uni.$u.route({
								type:"redirectTo",
								url: "/pagesC/product/goodstype/edit",
								params:{
									ptypeid:this.ptypeid,
									stoid:this.stoid,
									xzstoid:result.data 
								}
							})
							

						}
					});
				} else {
					this.$refs.notice.show({
						type: "danger",
						message: result.msg,
						duration: 2000,

					});
				}
			})
				.catch(() => {
					uni.$u.toast("效验失败");
				});



		},

		async goodsinfo()
		{
			let data = {
				adminid:this.auth.id,
				ptypeid:this.ptypeid,
				xzstoid:this.xzstoid

			}
			let result = await this.$u.api.product.goodstypeinfo(data)
			if(result.code)
			{
				this.brank = result.data

				if(this.brank.thumb_text != "")
				{
					this.filelist = [
					{ url: this.brank.thumb_text }
				]
				}
				
				
				
			}
		}
		
	

	},
};
</script>
  
<style >
.secetion {
	padding: 20px;
}

.container {
	height: 100%;
	margin: 0 50rpx;
	box-sizing: border-box;
}

.u-avatar {
	margin: 80rpx auto 40rpx;
}

.vedio {
	text-align: center;
}

.u-vedio {
	margin: 80rpx auto 40rpx;
}

.xcx {
	width: 200rpx;
	height: 200rpx;
	margin: 80rpx auto 40rpx;
	border-radius: 100%;
	overflow: hidden;
}

.nickname {
	font-size: 14px;
	text-align: center;
}

.title {
	text-align: center;
	margin: 40rpx 0;
	font-size: 14rpx;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}

.vedio .u-upload {
	justify-content: center;
	align-items: center;
	margin: 0.3em auto;
}

.u-form-item__body__left__content__label.data-v-5e7216f1 {
	width: 75px;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}
.branktypename{
	text-align: center;
  }
</style>
  